using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


public partial class Search_PageSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidSearchType.Value = Request.QueryString["s"]; 
            txtSearch.Text = Request.QueryString["q"];
            hidReturnType.Value = Request.QueryString["c"];
            hidDataSource.Value = Request.QueryString["ds"];
            hidCondition.Value = HttpUtility.UrlDecode(Request.QueryString["x"]);
            btnSearch_Click(sender, e);
        }
    }

    protected void dg1_SelectedIndexChanged(object sender, EventArgs e)
    {
        returnSearchResult();
    }

    protected void returnSearchResult()
    {
        string script = "<script language='javascript' type='text/javascript'>";
        script += "    function returnResult(val, id) {";
        script += "        while(val==null||id==null);";
        script += "        if (window.showModalDialog) {";
        script += "            var retVal = new Array(val, id);";
        script += "            window.returnValue = retVal;";
        script += "        } else {";
        if (hidReturnType.Value == "CodeOnly")
        {
            script += "            window.opener.retVal.value=unescape(id);";
        }
        else if (hidReturnType.Value == "ValueOnly")
        {
            script += "            window.opener.retVal.value=unescape(val);";
        }
        else
        {
            //script += "            window.opener.retVal.value=unescape(id) + '-' + unescape(val);";
            script += "            window.opener.retVal.value=unescape(val);";
        }
        script += "            window.opener.retID.value=unescape(id);";
        script += "            if (window.opener.postBack) {";
        script += "                  window.opener.PostBackOnMainPage(window.opener.objBtn.replace(/_/g,'$'), '');";
        script += "            }";
        script += "        }";
        script += "        window.close();";
        script += "    }";
        script += String.Format("    returnResult(escape('{0}'), escape('{1}'));", HttpUtility.HtmlDecode(dg1.Rows[dg1.SelectedIndex].Cells[1].Text), HttpUtility.HtmlDecode(((LinkButton)dg1.Rows[dg1.SelectedIndex].Cells[0].Controls[0]).Text));
        script += "</script>";
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "__close", script);
    }

    protected void dg1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dg1.PageIndex = e.NewPageIndex;
        this.LoadGrid();
    }

    private void SettingGrid()
    {
        dg1.AutoGenerateColumns = false;

        ButtonField oBtnField = new ButtonField();
        oBtnField.CommandName = "Select";
        oBtnField.DataTextField = "SearchCode";
        oBtnField.HeaderText = "Kode";
        oBtnField.DataTextFormatString = "{0}";
        dg1.Columns.Add(oBtnField);

        BoundField oBndField = new BoundField();
        oBndField.DataField = "SearchValue";
        oBndField.HeaderText = "Nama";
        dg1.Columns.Add(oBndField);
    }

    private void LoadGrid()
    {
        ArrayList oList = new ArrayList();
        clsSearchSQL oSearchSQL = new clsSearchSQL();

        oList = oSearchSQL.GetAllData((SearchType)Enum.Parse(typeof(SearchType), hidSearchType.Value, true),
            txtSearch.Text, hidCondition.Value, 
            (ReturnType)Enum.Parse(typeof(ReturnType), hidReturnType.Value, true));
        dg1.DataSource = oList;

        HtmlGenericControl body = (HtmlGenericControl)this.FindControl("Body");
        if (oList.Count == 0)
        {
            body.Attributes.Add("onunload", "if (!window.showModalDialog) { if (window.opener.postBack) { window.opener.PostBackOnMainPage(window.opener.objBtn.replace(/_/g,'$'), ''); } }");
        }
        else
        {
            body.Attributes.Remove("onunload");
        }

        string[] a = new string[1];
        a[0] = "SearchCode";
        dg1.DataKeyNames = a;

        dg1.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (dg1.Columns.Count == 0)
        {
            this.SettingGrid();
        }
        this.LoadGrid();
        if (dg1.Rows.Count == 1)
        {
            dg1.SelectedIndex = 0;
            returnSearchResult(); 
        }
    }
}
